using System;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Collections.Generic;

namespace DaphneTranslator
{
	public class ConfigHelper
	{
		private string keyNodeConfig = "configuration";
		private string keyNodeRecentPaths = "recentPaths";
		private string keyNodeRecentPath = "recentPath";
		private string keyAttrRecentPath = "path";
		private string keyFilename = "config.xml";
		
		private const int  _lMaxRecentFiles = 4;
		List<string> _lstRecentPaths;
		
	
		
		public ConfigHelper ()
		{
			_lstRecentPaths = new List<string> ();		
			
			Load ();
		}
		
		public void RegisterDocument (string sPath)
		{
			if (_lstRecentPaths.Contains (sPath) == true)
				return;
			_lstRecentPaths.Add (sPath);
			if (_lstRecentPaths.Count > _lMaxRecentFiles)
				_lstRecentPaths.RemoveAt (0);
			
			Save ();
		}
		
		public List<string> GetRecentDocuments ()
		{
			return _lstRecentPaths;
		}
		
		private string GetConfigPath()
		{			
			return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), keyFilename);
		}
		
		#region Load
		
		private void Load ()
		{
			try {
				XmlDocument doc = new XmlDocument ();
				doc.Load (GetConfigPath ());
				
				var list = FindNodes (doc.LastChild, keyNodeRecentPaths);
				if (list.Count > 0) {
					var list2 = FindNodes (list [0], keyNodeRecentPath);
					foreach (XmlNode item in list2) {
						string sPath = GetAttributeValue (item, keyAttrRecentPath);
						if (string.IsNullOrWhiteSpace (sPath) == false)
							_lstRecentPaths.Add (sPath);
					}					
				}											
			}
			catch
			{
			}			
		}
		
		private List<XmlNode> FindNodes (XmlNode parent, string sNodeName)
		{
			List<XmlNode> lstNodes = new List<XmlNode> ();
			for (int i = 0; i < parent.ChildNodes.Count; i++) {
				XmlNode node = parent.ChildNodes.Item (i);
				if (node.Name == sNodeName)
					lstNodes.Add (node);
			}
			
			return lstNodes;
		}
		
		private string GetAttributeValue (XmlNode node, string sAttributeName)
		{
			for (int i = 0; i < node.Attributes.Count; i++) {
				XmlAttribute attr = node.Attributes[i];
				if (attr.Name == sAttributeName)
					return attr.Value;
			}
			
			return string.Empty;
		}				
		
		#endregion
		
		
		#region Save
		
		private void Save ()
		{
			XmlDocument doc = new XmlDocument ();
			XmlElement root = AddElement (doc, keyNodeConfig, null);
			doc.AppendChild (root);
			
			XmlElement recentSection = AddElement (doc, keyNodeRecentPaths, root);
			foreach (string sPath in _lstRecentPaths) {
				XmlElement xmlPath = AddElement (doc, keyNodeRecentPath, recentSection);
				AddAttribute (doc, xmlPath, keyAttrRecentPath, sPath);
			}
			
			try {
				string sConfigPath = GetConfigPath ();
				doc.Save(sConfigPath);
			}
			catch
			{
			}					
		}
		
		
		private  XmlElement AddElement (XmlDocument _doc, string sName, XmlElement parent)
		{
			XmlElement element = _doc.CreateElement (sName);
			if(parent != null)
				parent.AppendChild (element);		
			return element;
		}
		
		private void AddAttribute (XmlDocument _doc, XmlNode node, string sAttrName, string sAttrValue)
		{
			XmlAttribute attribute = _doc.CreateAttribute (sAttrName);
			attribute.Value = sAttrValue;
			node.Attributes.Append (attribute);
		}		
		
		#endregion
		
		
		#region Singleton
			
		public static ConfigHelper Instance()
		{
			if(_Instance != null)
				return _Instance;
			_Instance = new ConfigHelper();
			return _Instance;
		}
		
		private static ConfigHelper _Instance = null;
		
		#endregion
		
	}
}

